
function update($component, state) { // 更新state  因为react里面是通过setState来更新数据的  小程序是通过setData来更新数据的  update是将两者相对应的
  $component.state = {...$component.state, ...state};
  // react render内部可能会增加变量 所以通过createData方法来处理render内部新增的变量
  const data = $component.createData(state);
  $component.state = data;
  $component.$scope.setData(data);
}
class Component {
  constructor() {
    this.state = {};
  }
  setState(state) {
    console.log(state, '调用');
    update(this.$scope.$component, state)
  }
  _init(scope) { // 把小程序的page|component实例挂到当前组件实例上  后续update的时候会调用小程序实例上方法  比如update会调用setData方法
    this.$scope = scope;
  }
}
function createPage (ComponentClass) {
  let option = {};
  const instance = new ComponentClass();
  option.data = instance.state;
  option = {
    ...option,
    onLoad: function() {
      this.$component = new ComponentClass();
      this.$component._init(this);
    },
    onReady: function() {
      if(this.$component.componentDidMount && typeof this.$component.componentDidMount === 'function') {
        this.$component.componentDidMount();
      }
    }
  }
  const events = ComponentClass.$$events;
  if (events) { // 事件处理
    events.forEach((item) => {
      option[item] = function() {
        this.$component[item].call(this.$component, ...arguments);
      }
    })
  }
  return option;
}
module.exports = {
  Component,
  createPage,
};
